using System;
using System.Collections.Generic;
using UnityEngine;

public class ArenaCustomizer : MonoBehaviour
{
	[Serializable]
	public class ModelTransformDefinition
	{
		public string name;

		public Transform transform;
	}

	public List<PersonaModel> personas = new List<PersonaModel>();

	public List<ModelTransformDefinition> modelTransforms = new List<ModelTransformDefinition>();

	public TableTennisMatchController matchController;

	public string configFilename = "ArenaCustomizations";

	public bool applyContinuously;

	public string arenaName;

	public void ApplyCustomization(ObjectLookCustomization customization, TableTennisMatchController matchController)
	{
		foreach (ObjectLookCustomization.MaterialCustomization customization2 in customization.customizations)
		{
			customization2.ApplyCustomization(GetTransformListForNames(customization2.namesOfObjectsToCustomize, matchController));
		}
		this.matchController = matchController;
	}

	public List<Transform> GetTransformListForNames(List<string> names, TableTennisMatchController matchController)
	{
		List<Transform> list = new List<Transform>();
		foreach (string name in names)
		{
			if (name.ToUpper().Equals("ambient".ToUpper()))
			{
				Debug.Log("Ambient");
				if (matchController != null)
				{
					foreach (TableTennisPlayerBase player in matchController.players)
					{
						PersonaModel component = player.gameObject.GetComponent<PersonaModel>();
						if (component != null)
						{
							Debug.Log("Ambient Model Parts " + component.GetPartsToUpdateForAmbient().Count);
							list.AddRange(component.GetPartsToUpdateForAmbient());
						}
					}
				}
				foreach (PersonaModel persona in personas)
				{
					list.AddRange(persona.GetPartsToUpdateForAmbient());
				}
				continue;
			}
			foreach (ModelTransformDefinition modelTransform in modelTransforms)
			{
				if (name.ToUpper().Equals(modelTransform.name.ToUpper()) || modelTransform.transform.name.ToUpper().Equals(name.ToUpper()))
				{
					list.Add(modelTransform.transform);
					break;
				}
			}
		}
		return list;
	}

	private void Update()
	{
		if (applyContinuously)
		{
			Debug.Log("Continuously " + configFilename);
			ArenaCustomizations arenaCustomizations = Resources.Load<ArenaCustomizations>(configFilename);
			if (arenaCustomizations == null)
			{
				arenaCustomizations = ScriptableObjectSingleton<ArenaCustomizations>.instance;
			}
			ObjectLookCustomization arenaForName = arenaCustomizations.GetArenaForName(arenaName);
			if (arenaForName != null)
			{
				Debug.Log("Apply");
				ApplyCustomization(arenaForName, matchController);
			}
		}
	}
}
